package com.cqjtu.drive.service.impl;

import com.cqjtu.drive.mapper.StudentMapper;
import com.cqjtu.drive.mapper.UserMapper;
import com.cqjtu.drive.pojo.Student;
import com.cqjtu.drive.response.Response;
import com.cqjtu.drive.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.plaf.ListUI;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private UserMapper userMapper;
    @Override
    public Response<Student> getStudentById(String studentId) {
        Student student = studentMapper.getStudentById(studentId);
        if (student != null) {
            return new Response<>(true, "Student found", student);
        } else {
            return new Response<>(false, "Student not found");
        }
    }

    @Override
    public Response<Student> getStudentByuserId(String userId) {
        Student student = studentMapper.getStudentByuserId(userId);
        if (student != null) {
            return new Response<>(true, "Student found", student);
        } else {
            return new Response<>(false, "Student not found");
        }
    }

    @Override
    public Response<String> insertStudent(Student student) {
        try {
            studentMapper.insertStudent(student);
            return new Response<>(true, "Student inserted successfully");
        } catch (Exception e) {
            return new Response<>(false, "Failed to insert student: " + e.getMessage());
        }
    }

    @Override
    public Response<String> updateStudent(Student student) {
        try {
            studentMapper.updateStudent(student);
            return new Response<>(true, "Student updated successfully");
        } catch (Exception e) {
            return new Response<>(false, "Failed to update student: " + e.getMessage());
        }
    }

    @Override
    public Response<String> deleteStudent(String studentId) {
        try {
            Student student = studentMapper.getStudentById(studentId);
            studentMapper.deleteStudent(studentId);
            userMapper.deleteUser(student.getUserId());
            return new Response<>(true, "Student deleted successfully");
        } catch (Exception e) {
            return new Response<>(false, "Failed to delete student: " + e.getMessage());
        }
    }

    @Override
    public Response<String> deleteBysName(String sName) {
        try {
            List<Student> students = studentMapper.getStudentsByName(sName);
            studentMapper.deleteStudent(sName);
            for(Student student : students){
                userMapper.deleteUser(student.getUserId());
            }
            return new Response<>(true, "Student deleted successfully");
        } catch (Exception e) {
            return new Response<>(false, "Failed to delete student: " + e.getMessage());
        }
    }

    @Override
    public Response<List<Student>> getAllStudents() {
        List<Student> students = studentMapper.getAllStudents();
        if (students != null && !students.isEmpty()) {
            return new Response<>(true, "Students retrieved successfully", students);
        } else {
            return new Response<>(false, "No students found");
        }
    }

    @Override
    public Response<List<Student>> getBysName(String sName) {
        List<Student> students = studentMapper.getStudentsByName(sName);
        if (students != null && !students.isEmpty()) {
            return new Response<>(true, "Students retrieved successfully", students);
        } else {
            return new Response<>(false, "No students found");
        }
    }
}
